package com.itheima.controller;

import com.itheima.common.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.Student;
import com.itheima.service.SetmealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

@Controller
@Slf4j
public class HelloController {
    //注入service
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private Configuration configuration;


    @GetMapping("/test2")
    public void test(HttpServletRequest request, HttpServletResponse response) throws IOException, TemplateException {
        //freemarker的模板对象，获取模板
        Template template = configuration.getTemplate("02-list.ftl");
        Map params = getData();
        //合成
        //第一个参数 数据模型
        //第二个参数  输出流
        String absolutePath = new File("").getAbsolutePath() +
                "/bxg-health-mobile/src/main/resources/static/list.html";
        template.process(params, new FileWriter(absolutePath));
        System.out.println("生成静态页面成功");
//        response.sendRedirect("http://localhost/list.html");
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("生成静态页面成功");
    }
    private Map getData() {
        Map<String, Object> map = new HashMap<>();

        //小强对象模型数据
        Student stu1 = new Student();
        stu1.setName("小强");
        stu1.setAge(18);
        stu1.setMoney(1000.86f);
        stu1.setBirthday(new Date());

        //小红对象模型数据
        Student stu2 = new Student();
        stu2.setName("小红");
        stu2.setMoney(200.1f);
        stu2.setAge(19);

        //将两个对象模型数据存放到List集合中
        List<Student> stus = new ArrayList<>();
        stus.add(stu1);
        stus.add(stu2);

        //向map中存放List集合数据
        map.put("stus", stus);


        //创建Map数据
        HashMap<String, Student> stuMap = new HashMap<>();
        stuMap.put("stu1", stu1);
        stuMap.put("stu2", stu2);
        //向map中存放Map数据
        map.put("stuMap", stuMap);

        //返回Map
        return map;
    }



    @GetMapping("/basic")
    public String test(Model model) {


        //1.纯文本形式的参数
        model.addAttribute("name", "freemarker");
        //2.实体类相关的参数

        Student student = new Student();
        student.setName("小明");
        student.setAge(18);
        model.addAttribute("stu", student);

        return "01-basic";
    }





//
//    @GetMapping("/setmeal/getSetmeal")
////    @ApiOperation("查询所有套餐")
//    public String getSetmeal(Model model){
//        log.info("查询所有套餐");
//        List<Setmeal> setmealAll = setmealService.getSetmealAll();
//
//        model.addAttribute("setmealAll", setmealAll);
//        return "setmeal";
//    }
//
//    /**
//     * author: MaShuNing
//     * 根据套餐id查询套餐
//     * @param id
//     * @return
//     */
//    @GetMapping("/setmeal/setmealDetail")
//    @ApiOperation("根据套餐id查询套餐")
//    public void findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
////    public String findById(@RequestParam Integer id, Model model){
//        log.info("查询套餐id ， 放入List ， 生成静态页面");
////        Setmeal setmeal = setmealService.findById(id);
////
////        model.addAttribute("setmeal", setmeal);
//        //查询套餐id ， 放入List ， 生成静态页面
//        List<Setmeal> list = setmealService.getSetmealAll();
//        setmealService.generateMobileSetmealDetailHtml(list);
//
//        System.out.println("生成静态页面成功");
//        response.setContentType("text/html;charset=utf-8");
//        response.getWriter().write("生成静态页面成功");
//    }



}
